package com.hanserwei.community.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanserwei.community.entity.common.ApiResponse;
import com.hanserwei.community.entity.request.meter.MeterRecordQueryRequest;
import com.hanserwei.community.entity.request.meter.MeterRecordSaveRequest;
import com.hanserwei.community.entity.vo.MeterRecordVO;
import com.hanserwei.community.enums.ResponseCodeEnum;
import com.hanserwei.community.service.MeterRecordService;
import com.hanserwei.community.uitils.ThrowUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/meter/record")
@Tag(name = "MeterRecordController", description = "抄表管理接口")
@RequiredArgsConstructor
public class MeterRecordController {

    private final MeterRecordService meterRecordService;

    @Operation(summary = "分页查询抄表记录")
    @PostMapping("/search")
    public ApiResponse<Page<MeterRecordVO>> search(@RequestBody MeterRecordQueryRequest request) {
        ThrowUtils.throwIf(request == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        Page<MeterRecordVO> page = meterRecordService.pageMeterRecords(request);
        return ApiResponse.success("查询成功！", page);
    }

    @Operation(summary = "新增抄表记录")
    @PostMapping("/add")
    public ApiResponse<Boolean> add(@RequestBody MeterRecordSaveRequest request) {
        meterRecordService.createMeterRecord(request);
        return ApiResponse.success("新增成功！", true);
    }

    @Operation(summary = "更新抄表记录")
    @PostMapping("/update/{id}")
    public ApiResponse<Boolean> update(@PathVariable Integer id, @RequestBody MeterRecordSaveRequest request) {
        meterRecordService.updateMeterRecord(id, request);
        return ApiResponse.success("更新成功！", true);
    }

    @Operation(summary = "删除抄表记录")
    @DeleteMapping("/delete")
    public ApiResponse<Boolean> delete(@RequestParam("id") Integer id) {
        ThrowUtils.throwIf(id == null || id <= 0, ResponseCodeEnum.INVALID_PARAMETER, "记录ID参数错误！");
        boolean result = meterRecordService.removeById(id);
        ThrowUtils.throwIf(!result, ResponseCodeEnum.SYSTEM_ERROR, "删除失败！");
        return ApiResponse.success("删除成功！", true);
    }

    @Operation(summary = "批量删除抄表记录")
    @DeleteMapping("/deleteBatch")
    public ApiResponse<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        ThrowUtils.throwIf(ids == null || ids.isEmpty(), ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        boolean result = meterRecordService.removeBatchByIds(ids);
        ThrowUtils.throwIf(!result, ResponseCodeEnum.SYSTEM_ERROR, "删除失败！");
        return ApiResponse.success("删除成功！", true);
    }

    @Operation(summary = "查询抄表记录详情")
    @GetMapping("/{id}")
    public ApiResponse<MeterRecordVO> detail(@PathVariable Integer id) {
        MeterRecordVO detail = meterRecordService.getMeterRecordDetail(id);
        return ApiResponse.success("查询成功！", detail);
    }
}
